<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="虚拟中心名称" v-model="listQuery.centerName">
      </el-input>

      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="真实机构名称" v-model="listQuery.orgName">
      </el-input>

      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">添加</el-button>
    </div>

    <el-table :data="list" v-loading.body="listLoading" element-loading-text="死命加载中..." border fit highlight-current-row>
      <el-table-column align="center" label='序号' width="95">
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="虚拟中心名称">
        <template slot-scope="scope">
          {{scope.row.centerName}}
        </template>
    </el-table-column>
      <el-table-column label="所属机构名称" width="310" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.orgName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类" width="210" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.category=='0'">父级机构</span>
          <span v-if="scope.row.category=='1'">子级机构</span>
        </template>
      </el-table-column>
      <el-table-column label="胸痛ID" width="210" align="center">
        <template slot-scope="scope">
          {{scope.row.reportHospitalId}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="250">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="handleModify(scope.row.id,'published')">更新
          </el-button>
          <el-button v-if="scope.row.status!='deleted'" size="small" type="danger" @click="handleRemove(scope.row.id, scope.$index,'deleted')">删除
          </el-button>
          <el-button size="small" type="info" @click="handleToDetail(scope.row)">科室
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.curPage"
        :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form class="small-space" :model="temp" :rules="rules" ref="temp" label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
        
        <el-form-item label="虚拟名称" prop="centerName">
          <el-input v-model="temp.centerName"></el-input>
        </el-form-item>
        <el-form-item label="所属机构" prop="orgName">
          <el-autocomplete click="inline-input" v-model="temp.orgName" :fetch-suggestions="queryOrgs" placeholder="请输入正确的机构名称" :trigger-on-focus="false" @select="handleSelect">
          </el-autocomplete>
        </el-form-item>
        <el-form-item label="机构类别" prop="category                                                                                                                                                    ">
          <el-select class="filter-item" v-model="temp.category" placeholder="请选择">
            <el-option label="父机构" value="0">
            </el-option>
            <el-option label="子机构" value="1">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="胸痛ID">
          <el-input v-model="temp.reportHospitalId"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('temp')">确 定</el-button>
        <el-button v-else type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getList, requestOrgs, saveOrUpdate, remove, get} from '@/api/virtualcenters'
import waves from '@/directive/waves/index.js' // 水波纹指令
export default {
  name: 'virtualcenters',
  directives: {
    waves
  },
  data() {
    var checkOrg = (rule, value, callback) => {
      console.log(this.temp.orgCode)
      if (!this.temp.orgCode) {
        callback(new Error('请输入已存在的所属机构'))
      } else {
        callback();
      }
    }
    return {
      list: null,
      total: null,
      listQuery: {
          centerName: '',
          orgName: '',
          curPage: 0,
          pageSize: 10
      },
      orgs: [],
      listLoading: true,
      temp: {
        id: undefined,
        centerName: '',
        orgName: '',
        orgCode: '',
        category: '',
        reportHospitalId: ''
      },
      rules: {
        centerName: [{required: true, message: '请输入虚拟中心名称', trigger: 'blur'}],
        category: [{required: true, message: '请选择机构类型', trigger: 'change'}],
        orgName: [{validator: checkOrg, trigger: 'change'}]
        //orgName: [{required: true, message: '请输入正确的机构名称', trigger: 'change'}]
      },
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogFormVisible: false,
      dialogStatus: ''
        
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.result.list
        this.total = response.result.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.curPage = val
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.curPage = 1
      this.fetchData()
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleModify(id) {
      this.dialogStatus = 'modify'
      this.dialogFormVisible = true
      get(id).then(rs => this.temp = rs.result).catch(e => console.log(e))
    },
    handleRemove(id, index, status) {
      remove(id).then( rs => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.list.splice(index, 1)
      }
        
      ).catch(e => console.log(e))
    },
    handleToDetail(row) {
      alert('click dept')
      this.$router.push({path: '/virtualdepts', query:{centerId:row.id, centerName:row.centerName, orgCode: row.orgCode}})
    },
    resetTemp() {
      this.temp = {
        id: '',
        centerName: '',
        orgName: '',
        orgCode: '',
        category: '',
        reportHospitalId: ''
      }
    },
    update() {
      this.dialogFormVisible = false
      saveOrUpdate(this.temp).then(rs => {
        this.$notify({
        title: '成功',
        message: '更新成功',
        type: 'success',
        duration: 2000
      })
      //刷新列表
      this.fetchData()
      }).catch(e => console.log(e))
      
    },
    create(formName) {
      this.$refs[formName].validate((valid) => {
        console.log(valid)
        if (valid) {
          saveOrUpdate(this.temp).then(rs => {
            this.dialogFormVisible = false
            this.$notify({
            title: '成功',
            message: '创建成功',
            type: 'success',
            duration: 2000
            })
            //刷新列表
            this.fetchData()
          }).catch(e => console.log(e))
          
        } else {
          alert('false')
        }
      })
    },
    queryOrgs(queryString, cb) {
      requestOrgs(queryString).then(rs => {
        this.orgs = rs.result
        for (let org of this.orgs) {
          org['value'] = org.orgName
        }
        cb(this.orgs)
      }).catch(e => console.log('requet org list error:' + e))
    },
    handleSelect(item) {
      console.log("handleSelect")
      this.temp.orgCode = item.orgCode
      console.log(this.temp)
    }
  }
}
</script>
